package org.zn.mvcbase.rest.naming;


import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.core.io.ClassPathResource;
import org.zn.commons.collection.MapType
import org.zn.commons.collection.Maps

public class LocalXmlNamingIndexer implements RestNamingIndexer {
	
	SAXReader reader=new SAXReader()
	Map<String,Naming>  namingContext=Maps.newMap(MapType.ConcurrentMap)
	
	String xmlpath;
	String protocol="http"
	String domain;
	String port;
	String context;
	boolean gateway;
	
	public LocalXmlNamingIndexer() {
		// TODO Auto-generated constructor stub
	}
	
	public LocalXmlNamingIndexer(String xml){
		this.xmlpath=xml;
	}
	
	@Override
	public String gateWayDomain() {
		// TODO Auto-generated method stub
		return domain;
	}

	@Override
	public String gateWayPort() {
		// TODO Auto-generated method stub
		return port;
	}

	@Override
	public String gateWayContext() {
		// TODO Auto-generated method stub
		return context;
	}

	@Override
	public boolean hasGateWay() {
		// TODO Auto-generated method stub
		return gateway;
	}
	
	
	public String gateWayPath() {
		
		String value="$protocol://$domain"
		
		if(port) value="$value:$port"
		if(context) value="$value/$context"
		
		value
	}
	

	@Override
	public String lookupValue(String name) {
		// TODO Auto-generated method stub
		String value="$protocol://$domain"
		
		if(port) value="$value:$port"
		if(context) value="$value/$context"
	
		
		String[]  ns=name.split("/")
		
		Naming na=null
		
		if(ns.length>0) {
		na=namingContext.get(ns[0])
		
//			if(na.getValue()) {
//				value="$value/$na.value"
//			}
//			else {
//				value="$value/$na.name"
//			}
		
		}
		
		for(def i=1;i<ns.length;i++) {
			
		na=na?.get(ns[i])
	
		
		}
		
		if(na.getValue()) {
			value="$value/$na.value"
		}
		else {
			value="$value/$na.name"
		}
		
		
		return value;
		
	}

	@Override
	public Naming lookupNaming(String name) {
		// TODO Auto-generated method stub
		
		String[]  ns=name.split("/")
		
		Naming na=null
		
		if(ns.length>0) {
		na=namingContext.get(ns[0])
	
		}
		
		for(def i=1;i<ns.length;i++) {
			
		na=na?.get(ns[i])
	
		
		}
		
		return na;
	}

	@Override
	public void bind(String name, Naming naming) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void initialize() {
		// TODO Auto-generated method stub
		
		ClassPathResource cpr=new ClassPathResource(xmlpath)

			Document doc=reader.read(cpr.getInputStream())
			
		  	Element root=doc.getRootElement()
		  	
		  	Element gate=root.element("GateWay")
		  	
		  	if(gate) {
				  
			gateway=true
			domain=gate.attribute("domain").stringValue
			port=gate.attribute("port").stringValue
			context=gate.attribute("context").stringValue
		  		
			
		  	}
		  	
		  	
		 List<Element>  servicelist=root.elements("Service")
		
		 for (se in servicelist) {
			 
			 ServiceNaming sn=new ServiceNaming()
			 
			sn.setName(se.attribute("name").stringValue)
			
			List<Element> resList=se.elements "Resources"
			
			for (re in resList) {
				
				ResourcesNaming rn=putResourcesNaming(re,sn.getName())
				
				sn.put rn
				
			}
			
			namingContext.put(sn.name, sn)
			
		}
		
	}

	
	def ResourcesNaming putResourcesNaming(Element ele,String val) {
		
		ResourcesNaming rn=new ResourcesNaming();
		
		
	    String params=ele.attribute("params")?.stringValue
		
		if(params) {
			
		String[] pms=params.split(",")
			
		for(def i=0;i<pms.length;i++) {
		
			
		def v=pms[i];
			
		if(i==0) {
			params="?$v={$v}"
		}else {
			params+="&$v={$v}"
		}
			
		}
	
	
		
		}
		
		rn.setName(ele.attribute("name")?.stringValue)
		rn.setMethod(ele.attribute("method")?.stringValue)
		
		
		if(ele.attribute("accessible")) {
			
			String ab=ele.attribute("accessible").stringValue
			
			rn.setAccessible(ab.equals("true")?true:false)
		}
		
		rn.setValue(ele.attribute("value")?.stringValue)
		
		String value=null;
		
		if(rn.value) {
		 value="$val/$rn.value"
		}
		else {	
		value="$val/$rn.name"
		}
		
		
		if(params) {
			
			value+=params
			
		}
		
		println value
		
		rn.setValue(value);
		
		
		List<Element> list=ele.elements("Resources")
		
		if(list&&list?.size()>0) {
			
			for (iele in list) {

				rn.put(putResourcesNaming(iele,value))
				
			}
			
		}
		
		rn
		
	}
	

}
